/*
 *  Copyright 2011 yura.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.vcforms.editor.actions;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.opu.vcforms.editor.EditView;
import org.opu.vcforms.editor.TextView;

/**
 * 
 * @author yura
 */
public class EventBusEditAction {

    private List<EditAction> actions;
    private EditView view;
    private PropertyChangeListener changeListener;

    public EventBusEditAction(EditView view) {
        this.view = view;
        this.changeListener = new PropertyChangeListenerImpl(view.getChangeListener());
        this.actions = new ArrayList<EditAction>();

        actions.add(new SaveEditAction(view));
        actions.add(new PrintEditAction(view));
        actions.add(new EditEditAction(view));
        actions.add(null);
        actions.add(new CutEditAction(view));
        actions.add(new CopyEditAction(view));
        actions.add(new PasteEditAction(view));
        actions.add(null);
        actions.add(new UndoEditAction(view));
        actions.add(new RedoEditAction(view));
        actions.add(null);
        actions.add(new CloseViewEditAction(view));

        this.view.setChangeListener(this.changeListener);
    }

    public List<EditAction> getActions() {
        return Arrays.asList(actions.toArray(new EditAction[actions.size()]));
    }

    class PropertyChangeListenerImpl implements PropertyChangeListener {

        PropertyChangeListener parentListener;

        PropertyChangeListenerImpl(PropertyChangeListener changeListener) {
            parentListener = changeListener;
        }

        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            if (parentListener != null) {
                parentListener.propertyChange(evt);
            }

            TextView text = view.getSelectedText();

            // actions it`s array list, so it`s better to do fori
            for (int i = 0; i < actions.size(); i++) {
                EditAction editAction = actions.get(i);
                if (editAction instanceof SaveEditAction) {
                    editAction.setEnabled(!text.isSaved());
                } else if (editAction instanceof UndoEditAction) {
                    editAction.setEnabled(text.canUndo());
                } else if (editAction instanceof RedoEditAction) {
                    editAction.setEnabled(text.canRedo());
                }
            }
        }
    }

    /**
     * Return action by it name
     * @param name action name
     * @return edit action with specific name.
     */
    public EditAction getAction(String name){
        if (name == null){
            return null;
        }

        for (EditAction a : actions) {
            if (a != null) {
                if (a.getName().equals(name)) {
                    return a;
                }
            }
        }

        return null;
    }
}
